package Gastos;

import Personas.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author nan
 */
public class MedGastos implements ActionListener, KeyListener{

    private GUIGastos gastos = new GUIGastos();
    private Conexion.ConexionBD conectar = new Conexion.ConexionBD();
    private gastosListar gastoLis;

    public MedGastos(){
        gastos.setLocationRelativeTo(null);
        gastos.setActionListeners(this);
        gastos.setKeyListener(this);
        gastos.show();
    }
    public MedGastos(gastosListar gasLis){
        gastos.setLocationRelativeTo(null);
        gastos.setActionListeners(this);
        gastos.setKeyListener(this);
        gastos.show();
        gastoLis=gasLis;
        gastoLis.actualizarTabla();
    }

    public void setVisible(boolean mostrar){
        gastos.setVisible(mostrar);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == gastos.getjButtonAceptar()){
            insertGasto();
            gastoLis.actualizarTabla();
            gastos.dispose();
        }else{
            if(e.getSource() == gastos.getjButtonCancelar()){
                gastos.dispose();
            }
        }
    }

    private void insertGasto(){
        String a = (gastos.getjTextFieldGastos().getText());
            String aa = (gastos.getjTextFieldDescripcion().getText());
            String Fecha="";
            try {
                Fecha = Date.class.newInstance().toLocaleString(); //fecha del dia
            } catch (InstantiationException ex) {
                Logger.getLogger(MedGastos.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(MedGastos.class.getName()).log(Level.SEVERE, null, ex);
            }
            conectar.conectar();
            Statement st;
            try {
                st = conectar.getConn().createStatement();
                String consulta = "SELECT Max(\"IDGastos\") FROM \"Gastos\";";
                String insertar;
                ResultSet rs = st.executeQuery(consulta);
                int in = 0;
                while (rs.next()){
                    in = rs.getInt(1)+1;
                }
                st = conectar.getConn().createStatement();
                String insercion = "INSERT INTO \"Gastos\"(\"IDGastos\", \"Descripcion\", \"Fecha\", \"Gasto\") VALUES ("+in+",'"+aa+"','"+Fecha+"',"+Math.rint(Double.parseDouble(a))+")";
                st.execute(insercion);
                JOptionPane.showMessageDialog(null, "Se ha insertado el Gasto", "Elemento insertado", JOptionPane.INFORMATION_MESSAGE);
            } catch (SQLException ex) {
                JOptionPane.showMessageDialog(null, "Nose puede insertar", "ERROR", JOptionPane.ERROR_MESSAGE);
            }
            conectar.cerrar();
            gastos.dispose();
    }

    public void keyTyped(KeyEvent e) {
        
    }

    public void keyPressed(KeyEvent e) {   }

    public void keyReleased(KeyEvent e) {   }


   
}
